/**
 * 
 */
package com.csair.youth.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.lang.StringUtils;

import com.H2O.SCUTsports.ums.util.Encryption;
import com.csair.youth.constant.Constant;
import com.csair.youth.dao.impl.hibernate3.UserDaoImpl;
import com.csair.youth.database.User;
import com.xalp.encryption.utils.MD5Utils;

/**
 * @author mickey
 *
 */
public class UserServiceImpl {
	private UserDaoImpl udao;
	
	public UserDaoImpl getUdao() {
		return udao;
	}

	public void setUdao(UserDaoImpl udao) {
		this.udao = udao;
	}

	public void saveUser(User item){
		if(item == null) return;
		if(StringUtils.isEmpty(item.getId())) {
			item.setId(null);
			item.setPassword(generateDefaultPassword());
		}
		else{
			User tmpItem = getUser(item.getId());
			item.setPassword(tmpItem.getPassword());
			item.setLastLoginIp(tmpItem.getLastLoginIp());
			item.setLastLoginTime(tmpItem.getLastLoginTime());
		}
		udao.saveOrUpdate(item);
	}
	
	public void updateUserPassword(User item, String password){
		if(item == null || StringUtils.isEmpty(item.getId())) return;
		item.setPassword(password);
		udao.update(item);
	}
	
	public void updateUserLoginStatus(User item){
		if(item == null || StringUtils.isEmpty(item.getId())) return;
		udao.update(item);
	}
	
	public void deleteUserById(String id){
		if(!StringUtils.isEmpty(id)) udao.deleteById(id);
	}
	
	public void resetPasswordById(String id){
		if(!StringUtils.isEmpty(id)) udao.resetPasswordById(id, generateDefaultPassword());
	}
	
	public User getUser(String id){
		return udao.findById(id);
	}
	
	public User getUserByLoginName(String loginName){
		return udao.getUserByLoginName(loginName);
	}
	
	/**
	 * 默认密码生成，111111
	 * @return
	 * @throws Exception
	 */
	public String generateDefaultPassword() {
		String encryption = "0nwBlqX83wEysPW0m4kFbXFISOc+R2hxKq6aLw==";
		try {
			String plain = "111111";
			String md5 = MD5Utils.generatePasswordHex(plain);
			encryption = Encryption.creatPassword(md5);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally{
			
		}
		return encryption;
	}
	
	public String generatePassword(String text, boolean isEncryptedOnce){
		try{
			if(!isEncryptedOnce){
				text = MD5Utils.generatePasswordHex(text);
			}
			return Encryption.creatPassword(text);
		}catch(Exception e){
			e.printStackTrace();
			return generateDefaultPassword();
		}
	}
	
	public User login(String userName, String password) throws Exception {
		User user = getUserByLoginName(userName);
		if(user == null) return null;
		if(isPasswordRight(password, user.getPassword())) {
			return user;
		}
		else return null;
	}
	
	public boolean isPasswordRight(String given, String pwInDB) throws Exception {
		return Encryption.authenticatePassword(given, pwInDB);
	}
	
	public boolean isLoginNameUnique(String loginName, String uid){
		User tmpUser = udao.getUserByLoginName(loginName);
		//如果是update的话，登录名可能没改，这样如果发现是同一个用户，则不当作重复登录名
		if(tmpUser == null || uid.equals(tmpUser.getId())){
			return true;
		}
		else{
			return false;
		}
	}
	
	public void initGlobalUsers(){
		if(udao.getUserByLoginName(Constant.USER_AUTO_UPLOAD_LOGIN_NAME) == null){
			User user = new User();
			user.setName(Constant.USER_AUTO_UPLOAD_NAME);
			user.setLoginName(Constant.USER_AUTO_UPLOAD_LOGIN_NAME);
			user.setPassword("1");
			udao.save(user);
		}
	}
}
